import TopSearchPanel from '../MyBlog/RightContainer/Weibo/TopSearchPanel';
import {Collapse, Spin} from 'antd';
import WeaUtils from '../Component/wea-utils/';
import {WeaNewScroll } from 'ecCom';
import {WeaNewScrollPagination} from 'ecCom';
import WeiboListNR from '../Component/WeiboListNoRedux';
import {inject, observer} from 'mobx-react';
import {observable,action,toJS} from 'mobx';
const Panel = Collapse.Panel;
window.WeaUtils = WeaUtils;

import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

@inject('blogMyAttention')
@observer
class AttentionWeiboList extends React.Component {
    constructor(props) {
        super(props);
        this.getWeiboList = this.getWeiboList.bind(this);
        this.search = this.search.bind(this);
        this.queryNext = this.queryNext.bind(this);
        this.params = {};
        this.state = {
            reading: true
        };
    }
    componentDidMount() {
        if(this.props.blogMyAttention.status.type==''){
            this.props.blogMyAttention.getAttentionCountInfos();
        }
    }
    componentWillReceiveProps(nextProps) {
        let {status} = this.props.blogMyAttention;
        let {myCountList} = toJS(status);
    }
    search(params) {
        this.props.blogMyAttention.clearRightList();
        params.startDate = WeaUtils.DateFormat(params.startDate, 'yyyy-MM-dd');
        params.endDate = WeaUtils.DateFormat(params.endDate, 'yyyy-MM-dd');
        params.type = this.props.blogMyAttention.status.type;
        if (!params.content) {
            params.searching = false;
            this.params = params;
            this.props.blogMyAttention.getAttentionCountInfos(params);
        }
        else {
            params.groupId="all";
            params.currentPage=1;
            params.searching = true;
            this.params = params;
            this.props.blogMyAttention.searchWeiboList(params);
        }
    }
    queryNext() {


        let ended = !(this.props.searchWeiboList&&this.props.searchWeiboList.__hasData||false);
        let {status} = this.props.blogMyAttention;
		let {countLoaded} = toJS(status);
        if (!countLoaded || ended) return;
        this.params.currentPage++;
        this.props.blogMyAttention.searchWeiboList(this.params);
    }
    
    
    getWeiboList(args, myCountList = toJS(this.props.blogMyAttention.status.myCountList)) {
        if (!args || !myCountList)
            return;
        args.forEach((arg) => {
            let cdata = myCountList[arg];
            if (cdata && !cdata.loaded) {
                this.props.blogMyAttention.getAttentionWorkDateList({workDate:cdata.workdate,type:this.props.blogMyAttention.status.type,currentPage:1,pageSize:10}, arg);
            }
        });
    }


    remind(workdate) {
        this.props.blogMyAttention.sendRemindUnsubmitAll({workdate: workdate});
    }
    
    getSearchWeiboRender() {
        let {status} = this.props.blogMyAttention;
		let {userInfo,searchWeiboList,countLoaded} = toJS(status);
        let currentUserId = userInfo.id;
        let list = searchWeiboList;
        let ended = !list.__hasData;
        let hasData = list.discussList && list.discussList.length > 0;
        return (
            <div className="weiboDate">
                <WeaNewScrollPagination ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScrollPagination@flyx2q`} onScrollEnd={this.queryNext}>
                    {(countLoaded || hasData) && <WeiboListNR ecId={`${this && this.props && this.props.ecId || ''}_WeiboListNR@zj23ht`}
                        currentUserId={currentUserId}
                        records={searchWeiboList}
                        hasImage={false}
                        hasScroll={false}
                    />}
                    {countLoaded || <div className="pt20 align-center"><Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@7sxs4h`} /></div>}
                    {hasData && ended && <div className="endTip"><i className="icon-blog-ArrowheadUp" />{getLabel(384398,"没有微博了")}</div>}
                </WeaNewScrollPagination>
            </div>
        );
    }
    
    render() {
        let {status} = this.props.blogMyAttention;
        let {userInfo, myCountList, countLoaded} = toJS(status)
        if (myCountList.length > 0)
            this.getWeiboList([0], myCountList);

        let currentUserId = userInfo && userInfo.id;
        return (
            <div className="h100">
                <div className="weiboDateTop">
                    <TopSearchPanel ecId={`${this && this.props && this.props.ecId || ''}_TopSearchPanel@pqt38t`} ref="TopSearchPanel" requestData={this.search}/>
                </div>
                {this.params.searching ? this.getSearchWeiboRender() :
                    (countLoaded ? <div className="weiboDate panelWeibo">
                        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@4nrmap`}>
                            <Collapse ecId={`${this && this.props && this.props.ecId || ''}_Collapse@nxkjpy`} defaultActiveKey={['0']} onChange={this.getWeiboList}>
                                {!!myCountList && myCountList.map((item,i) => {
                                    let remind = this.remind.bind(this, item.workdate);
                                    return (
                                        <Panel ecId={`${this && this.props && this.props.ecId || ''}_Panel@oe21bq@${i}`} header={getPanelHeader(item, remind)} key={i}>
                                            {(!item.loaded) ? <div className="ml20 mt10 mb10"><Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@ukw451@${i}`} size="large" /></div>:
                                                <WeiboListNR ecId={`${this && this.props && this.props.ecId || ''}_WeiboListNR@555m2k@${i}`}
                                                    index = {i}
                                                    currentUserId={currentUserId}
                                                    records={item.data}
                                                    hasImage={false}
													moreLoading={status.morelading}
                                                    hasScroll={false}
                                                    blogType={i + 1}
                                                    addMore={this.props.blogMyAttention.addMore}
                                                    stype={status.type}
                                                    hasMore={item.hasMore}
                                                />
                                            }
                                        </Panel>
                                    );
                                })}
                            </Collapse>
                        </WeaNewScroll>
                    </div> :
                    <div className="weiboDate">
                        <div className="spin">
                            <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@spoqgw`} size="large"/>
                        </div>
                    </div>
                    )
                }
            </div>
        );
    }
}

let getPanelHeader = function(item, remind) {
    let {workdate, workdateLocal, unsubmit, isCanRemind} = item;
    let clickFunc = (event) => {
        event.stopPropagation();
        remind();
    };
    let _dateF1 = WeaUtils.DateFormat(workdateLocal,'M月dd日').replace('月',getLabel(33452,"月")).replace('日',getLabel(390,"日"));
    let _weekF1 = WeaUtils.DateFormat(workdateLocal,'周E').replace('周日',getLabel(16106,"周日")).replace('周一',getLabel(16100,"周一")).replace('周二',getLabel(16101,"周二"))
    .replace('周三',getLabel(16102,"周三")).replace('周四',getLabel(16103,"周四")).replace('周五',getLabel(16104,"周五")).replace('周六',getLabel(16105,"周六"));
    return (
        <span className="panelHeader">
            <span className="curdate">{(WeaUtils.DateFormat(new Date(), 'yyyy-MM-dd') == workdateLocal) && getLabel(15537,"今天")}</span>
            {_dateF1}
            <span className="ml5">{_weekF1}</span>
            {unsubmit > 0 && (<span className="orange pos-rel"><span className="dot">•</span><span className="dot-ml">{`${unsubmit}`}{getLabel(384466,"人未提交")}</span></span>)}
            <span className="icons">
                    {(isCanRemind == 1)&&<span className='icon-blog-remind' onClick={clickFunc}/>}
                <span className='icon-blog-down'/>
			</span>
        </span>
    );
};

// const mapStateToProps = state => {
//     const { myCountList,userInfo,searchWeiboList,countLoaded } = state.blogMyAttention;
//     return { myCountList,userInfo,searchWeiboList,countLoaded };
// };

// // 把 dispatch map 到组件的 props 上
// const mapDispatchToProps = dispatch => {
//     return {
//         actions: bindActionCreators(MyAttentionAction, dispatch)
//     };
// };
export default AttentionWeiboList;